/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.LicenseInfo;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentContact;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListForAccountPrintExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillListForAccountPrint.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		LicenseInfo lif = EncCommonHelper.getLicenseInfo(request);
		List list = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		String includeSub = (String) model.get("include_sub");
		if (includeSub == null) {
			includeSub = "";
		}

		String startDate = (String) model.get("start_date");
		if (startDate == null) {
			startDate = "";
		}

		String endDate = (String) model.get("end_date");
		if (endDate == null) {
			endDate = "";
		}

		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle redStyle = workbook.createCellStyle();
		HSSFFont redFont = workbook.createFont();
		redFont.setColor(10);
		redStyle.setFont(redFont);
		redStyle.setBorderBottom(7);
		redStyle.setBorderTop(7);
		redStyle.setBorderLeft(7);
		redStyle.setBorderRight(7);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(7);
		norStyle.setBorderTop(7);
		norStyle.setBorderLeft(7);
		norStyle.setBorderRight(7);
		HSSFCellStyle lastRedStyle = workbook.createCellStyle();
		lastRedStyle.setFont(redFont);
		lastRedStyle.setBorderBottom(2);
		lastRedStyle.setBorderTop(7);
		lastRedStyle.setBorderLeft(7);
		lastRedStyle.setBorderRight(7);
		HSSFCellStyle lastNorStyle = workbook.createCellStyle();
		lastNorStyle.setBorderBottom(2);
		lastNorStyle.setBorderTop(7);
		lastNorStyle.setBorderLeft(7);
		lastNorStyle.setBorderRight(7);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(2);
		boldStyle.setBorderTop(2);
		boldStyle.setBorderLeft(7);
		boldStyle.setBorderRight(7);
		HSSFCellStyle rightCellStyle = workbook.createCellStyle();
		rightCellStyle.setAlignment(3);
		int rowIndex = 0;
		byte colNum = 13;

		for (int os = 0; os < list.size(); ++os) {
			Object[] arr = (Object[]) list.get(os);
			List datas = (List) arr[0];
			Establishment est = (Establishment) arr[1];
			EstablishmentContact contact = (EstablishmentContact) arr[2];
			BigDecimal costTotal = (BigDecimal) arr[3];
			BigDecimal collectTotal = (BigDecimal) arr[4];
			BigDecimal adjustTotal = (BigDecimal) arr[5];
			BigDecimal favTotal = (BigDecimal) arr[6];
			BigDecimal remTotal = (BigDecimal) arr[7];
			HSSFRow r;
			HSSFCell c;
			int estStr;
			if (os > 0) {
				r = sheet.createRow(rowIndex);
				c = r.createCell(0);

				for (estStr = 1; estStr < colNum; ++estStr) {
					r.createCell((short) estStr);
				}

				++rowIndex;
			}

			r = sheet.createRow(rowIndex);
			r.setHeight(600);
			c = r.createCell(0);

			for (estStr = 1; estStr < colNum; ++estStr) {
				r.createCell((short) estStr);
			}

			c.setCellStyle(titleCellStyle);
			c.setCellValue(new HSSFRichTextString(lif.getEnterpriseName()
					+ " 业务对帐单"));
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex,
					(short) (colNum - 1)));
			++rowIndex;
			r = sheet.createRow(rowIndex);
			c = r.createCell(0);
			r.createCell(1);
			r.createCell(2);
			r.createCell(3);
			String arg59 = "订货单位：" + est.getEstName();
			if (includeSub.equals("1")) {
				arg59 = arg59 + "(包括下属单位)";
			}

			HSSFRichTextString ts = new HSSFRichTextString(arg59);
			ts.applyFont(0, 5, boldFont);
			ts.applyFont(5, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex, 3));
			c = r.createCell(4);
			r.createCell(5);
			r.createCell(6);
			r.createCell(7);
			ts = new HSSFRichTextString("日期：" + startDate + " 至 " + endDate);
			ts.applyFont(0, 3, boldFont);
			ts.applyFont(3, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 4, rowIndex, 7));
			c = r.createCell(8);
			r.createCell(9);
			r.createCell(10);
			r.createCell(11);
			ts = new HSSFRichTextString("合同金额：" + costTotal);
			ts.applyFont(0, 5, boldFont);
			ts.applyFont(5, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 8, rowIndex, 11));
			r.createCell(12);
			++rowIndex;
			r = sheet.createRow(rowIndex);
			c = r.createCell(0);
			r.createCell(1);
			r.createCell(2);
			r.createCell(3);
			ts = new HSSFRichTextString("已收金额：" + collectTotal);
			ts.applyFont(0, 5, boldFont);
			ts.applyFont(5, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex, 3));
			c = r.createCell(4);
			r.createCell(5);
			r.createCell(6);
			r.createCell(7);
			ts = new HSSFRichTextString("调账金额：" + adjustTotal);
			ts.applyFont(0, 5, boldFont);
			ts.applyFont(5, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 4, rowIndex, 7));
			c = r.createCell(8);
			r.createCell(9);
			r.createCell(10);
			r.createCell(11);
			ts = new HSSFRichTextString("尚欠金额：" + remTotal);
			ts.applyFont(0, 5, boldFont);
			ts.applyFont(5, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 8, rowIndex, 11));
			r.createCell(12);
			++rowIndex;
			r = sheet.createRow(rowIndex);
			c = r.createCell(0);
			r.createCell(1);
			r.createCell(2);
			r.createCell(3);
			ts = new HSSFRichTextString("地址：" + est.getAddress());
			ts.applyFont(0, 3, boldFont);
			ts.applyFont(3, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex, 3));
			c = r.createCell(4);
			r.createCell(5);
			r.createCell(6);
			r.createCell(7);
			String linkStr = "联系人：";
			String phoneStr = "电话：";
			if (contact != null) {
				linkStr = linkStr + contact.getContactName();
				if (contact.getPhone() != null) {
					phoneStr = phoneStr + contact.getPhone() + " ";
				}

				if (contact.getMobile() != null) {
					phoneStr = phoneStr + contact.getMobile();
				}
			}

			ts = new HSSFRichTextString(linkStr);
			ts.applyFont(0, 4, boldFont);
			ts.applyFont(4, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 4, rowIndex, 7));
			c = r.createCell(8);
			r.createCell(9);
			r.createCell(10);
			r.createCell(11);
			ts = new HSSFRichTextString(phoneStr);
			ts.applyFont(0, 3, boldFont);
			ts.applyFont(3, ts.length(), normalFont);
			c.setCellValue(ts);
			sheet.addMergedRegion(new Region(rowIndex, 8, rowIndex, 11));
			r.createCell(12);
			++rowIndex;
			r = sheet.createRow(rowIndex);
			c = r.createCell(0);

			for (int item = 1; item < colNum; ++item) {
				r.createCell((short) item);
			}

			c.setCellValue(new HSSFRichTextString(""));
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex,
					(short) (colNum - 1)));
			++rowIndex;
			r = sheet.createRow(rowIndex);
			c = r.createCell(0);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("日期"));
			c = r.createCell(1);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("单号"));
			c = r.createCell(2);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("总金额"));
			c = r.createCell(3);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("项目类型"));
			c = r.createCell(4);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("内容"));
			c = r.createCell(5);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("材质"));
			c = r.createCell(6);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("规格"));
			c = r.createCell(7);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("单位"));
			c = r.createCell(8);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("数量"));
			c = r.createCell(9);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("单价"));
			c = r.createCell(10);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("金额"));
			c = r.createCell(11);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("制作要求"));
			c = r.createCell(12);
			c.setCellStyle(boldStyle);
			c.setCellValue(new HSSFRichTextString("部门/经手人"));
			++rowIndex;
			long preId = 0L;
			long nextId = 0L;
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

			int countStr;
			for (countStr = 0; countStr < datas.size(); ++countStr) {
				Object[] objs;
				if (datas.size() > countStr + 1) {
					objs = (Object[]) datas.get(countStr + 1);
					nextId = ((BusinessBillItem) objs[0]).getBillId();
				} else {
					nextId = 0L;
				}

				objs = (Object[]) datas.get(countStr);
				BusinessBillItem arg60 = (BusinessBillItem) objs[0];
				String estName = (String) objs[1];
				String billCode = (String) objs[2];
				Date billDate = (Date) objs[3];
				BigDecimal cost = (BigDecimal) objs[4];
				String linkman = (String) objs[5];
				String collected = (String) objs[6];
				BigDecimal amount;
				if (!arg60.getPriceType().equals("AREA")
						&& !arg60.getPriceType().equals("LENGTH")) {
					amount = arg60.getAmount();
				} else {
					amount = arg60.getSquareNum();
				}

				String unit = "";
				String spe = arg60.getSpecification();
				if (arg60.getPriceType().equals("AREA")) {
					unit = "平方米";
					spe = spe
							+ " x "
							+ EncCommonHelper
									.parseBigDecimal(arg60.getAmount());
				} else if (arg60.getPriceType().equals("LENGTH")) {
					if (arg60.getLengType().equals("M")) {
						unit = "米";
					} else {
						unit = "公分";
					}

					spe = spe
							+ " x "
							+ EncCommonHelper
									.parseBigDecimal(arg60.getAmount());
				} else if (arg60.getPriceType().equals("AMOUNT")) {
					unit = arg60.getUnit();
				}

				String lmStr = "";
				if (includeSub.equals("1") && !est.getEstName().equals(estName)) {
					lmStr = estName + " ";
				}

				lmStr = lmStr + linkman;
				r = sheet.createRow(rowIndex);
				c = r.createCell(0);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				if (preId != arg60.getBillId()) {
					c.setCellValue(new HSSFRichTextString(sf.format(billDate)));
				}

				c = r.createCell(1);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				if (preId != arg60.getBillId()) {
					c.setCellValue(new HSSFRichTextString(billCode));
				}

				c = r.createCell(2);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				if (preId != arg60.getBillId()) {
					c.setCellValue(cost.doubleValue());
				}

				c = r.createCell(3);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(EncCommonHelper
						.getSimpleObjectName(mtList, arg60.getMakeTypeId())));
				c = r.createCell(4);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(arg60
						.getFinishedContent()));
				c = r.createCell(5);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(arg60.getProName()));
				c = r.createCell(6);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(spe));
				c = r.createCell(7);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(unit));
				c = r.createCell(8);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(amount.doubleValue());
				c = r.createCell(9);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(arg60.getUnitPrice().doubleValue());
				c = r.createCell(10);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(arg60.getCost().doubleValue());
				c = r.createCell(11);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				c.setCellValue(new HSSFRichTextString(arg60.getMakeRequire()));
				c = r.createCell(12);
				this.setDataStyle(c, collected, arg60.getBillId(), nextId,
						redStyle, norStyle, lastRedStyle, lastNorStyle);
				if (preId != arg60.getBillId()) {
					c.setCellValue(new HSSFRichTextString(lmStr));
				}

				preId = arg60.getBillId();
				++rowIndex;
			}

			r = sheet.createRow(rowIndex);
			c = r.createCell(0);

			for (countStr = 1; countStr < colNum; ++countStr) {
				r.createCell((short) countStr);
			}

			c.setCellStyle(rightCellStyle);
			String arg61 = "";
			if (favTotal.compareTo(new BigDecimal(0)) != 0) {
				arg61 = "小计金额：" + favTotal.add(costTotal) + "元  扣除优惠金额："
						+ favTotal + "元  ";
			}

			arg61 = arg61 + "合同金额：" + costTotal + "元";
			c.setCellValue(new HSSFRichTextString(arg61));
			sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex,
					(short) (colNum - 1)));
			++rowIndex;
		}

		sheet.setColumnWidth(0, 3200);
		sheet.setColumnWidth(1, 5000);
		ServletOutputStream arg58 = response.getOutputStream();
		workbook.write(arg58);
		arg58.flush();
		arg58.close();
	}

	private void setDataStyle(HSSFCell c, String collected, long id,
			long nextId, HSSFCellStyle redStyle, HSSFCellStyle norStyle,
			HSSFCellStyle lastRedStyle, HSSFCellStyle lastNorStyle) {
		if (collected.equals("Y")) {
			if (id == nextId) {
				c.setCellStyle(redStyle);
			} else {
				c.setCellStyle(lastRedStyle);
			}
		} else if (id == nextId) {
			c.setCellStyle(norStyle);
		} else {
			c.setCellStyle(lastNorStyle);
		}

	}
}